package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminRepositoryImpl implements AdminRepository {

    private AdminMapper adminMapper;
    @Autowired
    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    public AdminPO selectAdminByPhone(String phone) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_phone", phone);
        return adminMapper.selectOne(queryWrapper);
    }

    public List<AdminPO> selectList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(query.getAccountPhone() != null,"account_phone", query.getAccountPhone());
        queryWrapper.eq(query.getNickname() != null,"nickname", query.getNickname());
        return adminMapper.selectList(queryWrapper);
    }

    public Long selectCount(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(query.getAccountPhone() != null,"account_phone", query.getAccountPhone());
        queryWrapper.eq(query.getNickname() != null,"nickname", query.getNickname());
        return adminMapper.selectCount(queryWrapper);
    }

    public void save(AdminPO po) {
        adminMapper.insert(po);
    }
}
